import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/login/login.vue'
import Home from '@/components/home/home.vue'
import Users from '@/components/users/users.vue'
import Right from '@/components/rights/right.vue'
import Role from '@/components/rights/role.vue'
import Params from '@/components/goods/params.vue'
import Categories from '@/components/goods/categories.vue'
import Goods from '@/components/goods/goods.vue'
import {Message} from 'element-ui'
import Reports from '@/components/reports/reports.vue'
import Welcome from '@/components/welcome.vue'
import Add from '@/components/goods/Add.vue'
import Orders from '@/components/orders/orders.vue'
// 插件注册
Vue.use(Router)
const routes = [
    {
        path:'/',
        redirect:"/login"
    },
    {
        name:'login',
        path:'/login',
        component:Login
    },
    {
        name:'home',
        path:'/home',
        component:Home,
        redirect:'/welcome',
        children:[
            {
                name:'users',
                path:'/users',
                component:Users
            },
            {
                name:'rights',
                path:'/rights',
                component:Right
            },
            {
                name:'roles',
                path:'/roles',
                component:Role
            },
            {
                name:'params',
                path:'/params',
                component:Params
            },
            {
                name:'categories',
                path:'/categories',
                component:Categories
            },
            {
                name:'goods',
                path:'/goods',
                component:Goods
            },
            {
                name:'reports',
                path:'/reports',
                component:Reports
            },
            {
                name:'welcome',
                path:'/welcome',
                component:Welcome
            },
            {
                name:'add',
                path:'/goods/add',
                component:Add
            },
            {
                name:'orders',
                path:'/orders',
                component:Orders
            }
        ]
    }
    
]
const router = new Router({
    routes
})
router.beforeEach((to,from,next)=>{
    if(to.path === "/login"){
        return next()
    }
    const token = window.sessionStorage.getItem("token")
    if(!token){
        Message.warning("请先登录!")
        return next('/login')
    }
    next()
})
export default router